var basePath = "http://localhost:8080/AboutMe";
var powerTable;
var powerModal;
var powerForm;

$(function() {
	powerModal = $('#powerModal');
	powerForm = $('#powerForm');
});
function loadTable(){
	powerTable = $('#powerTable').bootstrapTable({
		url : basePath + '/admin/powerList',
		idField : 'powerId',
		pagination : true,
		pageSize : 10,
		pageNumber : 1,
		contentType: "application/x-www-form-urlencoded",
		method : 'post',
		dataType : 'json',
		sidePagination: "server",
		toolbar : '#toolbar',
		undefinedText : '',
		clickToSelect : true,
		columns : [ {
			radio : true
		},{
			field : 'powerName',
			title : '权限姓名',
		}, {
			field : 'powerExp',
			title : '权限表达式'
		} ]
	});
}

function add(){
	clear('#powerForm');
	$('#addSave').show();
	$('#editSave').hide();
	powerModal.find('.modal-title').text('新增权限')
	powerModal.modal('show');
}

function del(){
	var row = $('#powerTable').treegrid('getSelected');
	$.ajax({
		url : basePath + '/admin/delPower',
		type : "POST" ,
		dataType:'json',
		data : {powerId : row.powerId},
		success : function(data){
			powerModal.modal('hide');
			$('#powerTable').treegrid('reload');
		}
	}); 
}

function save(stat){
	$.ajax({
		url : basePath + '/admin/savePower',
		type : "POST" ,
		dataType:'json',
		data : powerForm.serialize(),
		success : function(data){
			powerModal.modal('hide');
			$('#powerTable').treegrid('reload');
		}
	}); 
}
function edit(){
	var row = $('#powerTable').treegrid('getSelected');
	if(row){
		load('#powerForm',row);
		$('#addSave').hide();
		$('#editSave').show();
		powerModal.find('.modal-title').text('修改权限')
		powerModal.modal('show');
	}
}
function clear(selector){
	$('input,select,textarea', selector).each(function(){
		var t = this.type, tag = this.tagName.toLowerCase();
		if (t == 'text' || t == 'hidden' || t == 'password' || tag == 'textarea'){
			this.value = '';
//		} 
//		else if (t == 'file'){
//			var file = $(this);
//			if (!file.hasClass('textbox-value')){
//				var newfile = file.clone().val('');
//				newfile.insertAfter(file);
//				if (file.data('validatebox')){
//					file.validatebox('destroy');
//					newfile.validatebox();
//				} else {
//					file.remove();
//				}
//			}
		} else if (t == 'checkbox' || t == 'radio'){
			this.checked = false;
		} else if (tag == 'select'){
			this.selectedIndex = -1;
		}

	});
}
function load(target,data){
	var form = $(target);
	for(var name in data){
		var val = data[name];
		form.find('input[name="'+name+'"]').val(val);
		form.find('textarea[name="'+name+'"]').val(val);
		form.find('select[name="'+name+'"]').val(val);
	}
}